﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using uk.org.aspellclark.todolistppc;
using System.Xml;
using Google.GData.Spreadsheets;
using System.Collections;
using ConsoleApplication1;
using Google.GData.Client;

namespace SyncToolProgram
{
    public partial class Form1 : Form
    {

        #region Private Variables

        string UserName;
        string Password;
        string XMLFile = "";

        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (checkgoogleconnection())
            {
                lblworking.Visible = true;
                this.Refresh();
                Sync oSync = new Sync();
                UserName = textBox1.Text;
                Password = textBox2.Text;

                if (GetTasks(oSync))
                {
                    oSync.SaveTasksToXML(XMLFile);
                    lblworking.Visible = false;
                    this.Refresh();

                    MessageBox.Show("Completed");
                }
                else
                {
                    MessageBox.Show("Connection To Google Failed");
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (checkgoogleconnection())
            {
                lblworking.Visible = true;
                this.Refresh();
                Sync oSync = new Sync();
                UserName = textBox1.Text;
                Password = textBox2.Text;
                if (GetTasks(oSync))
                {
                    oSync.SaveTasksToGoogleSpreadSheet(UserName, Password);
                    lblworking.Visible = false;
                    this.Refresh();
                    MessageBox.Show("Completed");
                }
                else
                {
                    MessageBox.Show("Connection To Google Failed");
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkgoogleconnection())
            {
                Sync oSync = new Sync();
                lblworking.Visible = true;
                this.Refresh();
                UserName = textBox1.Text;
                Password = textBox2.Text;
                if (GetTasks(oSync))
                {
                    oSync.SaveTasksToGoogleSpreadSheet(UserName, Password);
                    oSync.SaveTasksToXML(XMLFile);
                    lblworking.Visible = true;
                    this.Refresh();
                    MessageBox.Show("Completed");
                }
                else
                {
                    MessageBox.Show("Connection To Google Failed");
                }
            }

        }

        private bool GetTasks(Sync oSync)
        {
            if (!oSync.GetTasksFromGoogleSpreadsheet(UserName, Password))
            {
                return false;
            }

            oSync.GetTasksFromXML(XMLFile);
            return true;
            
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            XMLFile = openFileDialog1.FileName;
        }

        private bool checkgoogleconnection()
        {
            if (XMLFile == "")
            {
                MessageBox.Show("No File Selected");
                return false;
            }
            if (textBox1.Text == "")
            {
                MessageBox.Show("No UserName Entered");
                return false;
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("No Password Entered");
                return false;
            }
            if(textBox3.Text == "")
            {
                MessageBox.Show("No Worksheet Entered");
                return false;
            }
            return true;
        }

    }
}
